package com.kun.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.Jedis;

import com.alibaba.fastjson.JSON;
import com.kun.bean.City;
import com.kun.util.ConnectionUtil;
import com.kun.util.RedisUtil;

public class CityDao {
	
	private static Connection conn;
	private static City city;
	
	/**
	 * 从数据库中获取数据集合
	 * @return
	 */
	public  List<City> getCites(){
		ConnectionUtil connectionUtil = new ConnectionUtil();
		conn = connectionUtil.getConn();
		Statement st = null;
		ResultSet rs = null;
		List<City> list = new ArrayList<City>();
		String sql = "select a.cityName cityName,a.ckey ckey from city a left join province b on a.proid=b.proID";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while(rs.next()){
				city = new City();
				city.setCityName(rs.getString(1));
				city.setNickName(rs.getString(2));
				list.add(city);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			ConnectionUtil.closeConn(rs, st, conn);
		}
		return list;
	}
	

	
	/**
	 * 
	 * @param cities
	 */
	public  void pushCities(){
		Jedis jedis = RedisUtil.getJedis();
		if(jedis.exists("cities")){
			//做append的操作
		} else {
			List<City> cities = getCites();
			String cityjson = null;
			if (cities != null && cities.size() > 0) {
				cityjson = JSON.toJSONString(cities);
			}
			jedis.set("cities", cityjson);
		}
	}

}
